package com.sdemo.utils;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ThreadPoolUtil {
	
	public static ExecutorService executors;
	
	public static ThreadPoolExecutor threadPoolExecutor;
	/**
	 * 默认线程池个数128个
	 * liuwei
	 * 2016-03-25 13:30:33
	 * **/
	public static ExecutorService getInstance() {
		if (null == executors) {
			executors = Executors.newFixedThreadPool(128);
		}
		return executors;
	}
	
	public static ThreadPoolExecutor getThreadPoolExecutorInstance() {
		if (null == threadPoolExecutor) {
			threadPoolExecutor= new ThreadPoolExecutor(2, 256, 10, TimeUnit.SECONDS,  new LinkedBlockingQueue<Runnable>(), new ThreadPoolExecutor.DiscardOldestPolicy());
		}
		return threadPoolExecutor;
	}
	
	/**
	 * @param threadNum 需要线程池个数大小
	 * liuwei
	 * 2016-03-25 13:30:33
	 * **/
	public static ExecutorService getInstance(int threadNum) {
		if (null == executors) {
			executors = Executors.newFixedThreadPool(threadNum);
		}
		return executors;
	}
	
	
	/**
	 * @param newCachedThreadPool形式
	 * liuwei
	 * 2016-03-25 13:30:33
	 * **/
	public static ExecutorService getNewCachedInstance() {
		if (null == executors) {
			executors = Executors.newCachedThreadPool();
		}
		return executors;
	}
}
